﻿
@{
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}

<body>
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a>首页</a>
            <a class="p_name"></a>
            <a>
                <cite class="c_name"></cite>
            </a>
        </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
        </a>
    </div>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            @if (ViewData["menu"] != null)
            {
                <div class="layui-col-md12">

                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <h1 class="x-red" style="font-size:20px;font-weight:bold;line-height:32px;margin-bottom:12px">开发人员操作页其他人员请勿操作!</h1>
                            @using yiran.Areas.Admin.Models.Entity
                            @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "addcategrory").Any())
                            {
                                <form class="layui-form layui-col-space5">
                                    <div class="layui-inline layui-show-xs-block">
                                        <input type="text" lay-verify="required" name="name" placeholder="分类名" autocomplete="off" required class="layui-input">
                                    </div>
                                    <div class="layui-inline layui-show-xs-block">
                                        <input type="text" lay-verify="required" name="path" placeholder="规则" autocomplete="off" required class="layui-input">
                                    </div>
                                    <div class="layui-inline layui-show-xs-block">
                                        <input type="text" name="icon" placeholder="图标" autocomplete="off" class="layui-input">
                                    </div>
                                    <div class="layui-inline layui-show-xs-block">
                                        <input type="text" name="remark" placeholder="备注" autocomplete="off" class="layui-input">
                                    </div>
                                    @Html.AntiForgeryToken()
                                    <div class="layui-inline layui-show-xs-block">
                                        <button class="layui-btn" lay-submit="" lay-filter="add"><i class="layui-icon"></i>增加</button>
                                    </div>
                                </form>
                            }
                        </div>
                        <div class="layui-card-header">
                            @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "removecategrory").Any())
                            {
                                <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                            }
                        </div>
                        <div class="layui-card-body scroll">
                            <table class="layui-table layui-form">
                                <thead>
                                    <tr>
                                        <th>
                                            <input type="checkbox" title="全选" name="" lay-filter="checkall" lay-skin="primary">
                                        </th>
                                        <th>分类名</th>
                                        <th>规则</th>
                                        <th>图标</th>
                                        <th>排序</th>
                                        <th>备注</th>
                                        <th>操作</th>
                                </thead>
                                <tbody>
                                    @{ int mindex = 0; }
                                    @foreach (var m in Model)
                                    {
                                        <tr>
                                            <td>
                                                <input title="@(++mindex)" type="checkbox" name="id" value="@m.id" lay-skin="primary">
                                            </td>
                                            <td>@m.name</td>
                                            <td>@m.path</td>
                                            <td><i class="iconfont">@Html.Raw(m.icon)</i></td>
                                            <td><input @(!(ViewData["menu"] as List<sys_menu>).Where(c => c.action == "setsork").Any() ? "disabled" : "") onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" type="text" value="@m.sork" data-value="@m.sork" data-id="@m.id" onblur="setSork(this)" id="sork" name="sork" required="" lay-verify="required" autocomplete="off" class="layui-input" /></td>
                                            <td>@m.remark</td>
                                            <td class="td-manage">
                                                @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "editcategrory").Any())
                                                {
                                                    <a title="编辑" onclick="xadmin.open('编辑','/admin/role/EditCategrory/@m.id')" href="javascript:;">
                                                        <i class="layui-icon">&#xe642;</i>
                                                    </a>
                                                }
                                                @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "removecategrory").Any())
                                                {
                                                    <a title="删除" onclick="member_del(this,'@m.id')" href="javascript:;">
                                                        <i class="layui-icon">&#xe640;</i>
                                                    </a>
                                                }
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                @Html.AntiForgeryToken()
            }
            else
            {
                <div class="layui-col-md12">
                    <partial name="Error" />
                </div>
            }


        </div>
</body>
<script>
    layui.use(['laydate', 'form'], function () {

        var laydate = layui.laydate;
        var form = layui.form;
        @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "addcategrory").Any()) {
            <text>
            form.on('submit(add)', function(data) {
                $.ajax({
                    url: '/Admin/Role/AddCategrory',
				    type: 'POST',
				    data: data.field,
				    dataType: 'json',
				    async: true,
                    success: function(data, status, xhr) {
                            if (data.code == 200) {
                                layer.msg(data.result, {
                                icon: 1,
                                time: 2000
                                }, function(){
                                    location.reload();
                                });
                            } else {
                                layer.msg(data.result, { icon: 2 });
                            }
                        },
                    error: function() {
                            layer.msg('出错了~', { icon: 2 });
                        }
                });
                return false;
            });
            </text>
        }
        // 监听全选
        form.on('checkbox(checkall)', function(data){
            if(data.elem.checked){
            $('tbody input').prop('checked',true);
                }else{
            $('tbody input').prop('checked',false);
                }
                form.render('checkbox');
                });
            //执行一个laydate实例
            laydate.render({
            elem: '#start' //指定元素
        });

            //执行一个laydate实例
            laydate.render({
            elem: '#end' //指定元素
        });
        });
     @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "removecategrory").Any()) {
          <text>
          /*用户-删除*/
          function member_del(obj,id){
              layer.confirm('确认要删除吗？', function (index) {
                    $.ajax({
                        url:'/Admin/Role/RemoveCategrory',
                        type: 'POST',
                        data: {"id[]":id,"__RequestVerificationToken":$("input[name=__RequestVerificationToken]").val()},
					    dataType: 'json',
					    async: true,
                        success: function (data, status, xhr) {
                            if (data.code == 200) {
                                layer.msg(data.result, {
                                    icon: 1,
                                    time: 2000
                                }, function(){
                                    location.reload();
                                });
                            } else {
                                layer.msg(data.result, { icon: 2 });
                            }
                        },
                        error: function () {
                            layer.msg("出错了~", { icon: 2 });
                        }
				    });
              });
          }
           </text>
           <text>
          function delAll (argument) {
            var ids = [];
            // 获取选中的id
            $('tbody input').each(function(index, el) {
                if($(this).prop('checked')){
                    ids.push($(this).val())
                }
            });
             if (ids.length @Html.Raw("<")= 0) {
                 layer.msg("请选择要删除的行", { icon: 2, time: 2000 });
                 return false;
             }
             layer.confirm('确认要删除吗？', function (index) {
                 $.ajax({
                    url:'/Admin/Role/RemoveCategrory',
                    type: 'POST',
                    data: {id:ids,"__RequestVerificationToken":$("input[name=__RequestVerificationToken]").val()},
				    dataType: 'json',
				    async: true,
                    success: function (data, status, xhr) {
                        if (data.code == 200) {
                            layer.msg(data.result, {
                                icon: 1,
                                time: 2000
                            }, function(){
                                location.reload();
                            });
                        } else {
                            layer.msg(data.result, { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg("出错了~", { icon: 2 });
                    }
			    });
            });
        }
        </text>
     }
    @if ((ViewData["menu"] as List<sys_menu>).Where(c => c.action == "setsork").Any()) {
            <text>
                function setSork(obj) {
                    var sork = $(obj).val();
                    var id = $(obj).attr("data-id")
                    if (sork != $(obj).attr("data-value")) {
                    layer.confirm('是否更改排序？', function(index) {
                        var data = {
                    id: id,
                            sork: sork,
                    "__RequestVerificationToken":$("input[name=__RequestVerificationToken]").val()
                       }
                $.ajax({
                    url: '/Admin/Role/setsork',
					type: 'POST',
					data: data,
					dataType: 'json',
					async: true,
                    success: function(data, status, xhr) {
                            if (data.code == 200) {
                                layer.msg(data.result, {
                                icon: 1,
                                time: 2000
                                }, function(){
                                    location.reload();
                                });
                            } else {
                                layer.msg(data.result, { icon: 2 });
                            }
                        },
                    error: function() {
                            layer.msg("出错了~", { icon: 2 });
                        }
                    });

                }, function() {
                $(obj).val($(obj).attr("data-value"));
                });
            }
        }
        </text>
    }

</script>

